home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / glchess / main.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  22.6 KB  |  752 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''
  5. '''
  6. __author__ = 'Robert Ancell <bob27@users.sourceforge.net>'
  7. __license__ = 'GNU General Public License Version 2'
  8. __copyright__ = 'Copyright 2005-2006  Robert Ancell'
  9. __all__ = [
  10.     'Application']
  11. import sys
  12. import os
  13. import errno
  14. from gettext import gettext as _
  15. import traceback
  16. import time
  17. import config
  18. import ui
  19. import gtkui
  20. import game
  21. import player
  22. import chess.board as chess
  23. import chess.lan as chess
  24. import chess.pgn as chess
  25. import ai
  26. import network
  27. import display
  28. import history
  29. from defaults import *
  30.  
  31. class PlayerTimer(ui.TimerFeedback):
  32.     '''
  33.     '''
  34.     
  35.     def __init__(self, game, colour, duration):
  36.         self.game = game
  37.         self.colour = colour
  38.         self.duration = duration
  39.         self.controller = game.application.ui.controller.addTimer(self, duration)
  40.  
  41.     
  42.     def onTick(self, t):
  43.         '''Called by ui.TimerFeedback'''
  44.         if self.colour is chess.board.WHITE:
  45.             self.game.view.controller.setWhiteTime(self.duration, t)
  46.         else:
  47.             self.game.view.controller.setBlackTime(self.duration, t)
  48.  
  49.     
  50.     def onExpired(self):
  51.         '''Called by ui.TimerFeedback'''
  52.         if self.colour is chess.board.WHITE:
  53.             self.game.getWhite().outOfTime()
  54.         else:
  55.             self.game.getBlack().outOfTime()
  56.  
  57.  
  58.  
  59. class ChessGame(game.ChessGame):
  60.     '''
  61.     '''
  62.     view = None
  63.     __movePlayer = None
  64.     __aiPlayers = None
  65.     __promotionMapping = {
  66.         'queen': chess.board.QUEEN,
  67.         'knight': chess.board.KNIGHT,
  68.         'bishop': chess.board.BISHOP,
  69.         'rook': chess.board.ROOK }
  70.     duration = 0
  71.     wT = None
  72.     bT = None
  73.     
  74.     def __init__(self, application, name):
  75.         """Constructor for a chess game.
  76.         
  77.         'application' is a reference to the glChess application.
  78.         'name' is the name of the game (string).
  79.         """
  80.         self.application = application
  81.         self.name = name
  82.         self._ChessGame__aiPlayers = []
  83.         self.fileName = None
  84.         self.inHistory = False
  85.         self.needsSaving = False
  86.         game.ChessGame.__init__(self)
  87.         self.view = display.View(self)
  88.         self.view.controller = application.ui.controller.setView(name, self.view)
  89.         self.view.updateRotation(animate = False)
  90.         self.view.showMoveHints(config.get('show_move_hints') is True)
  91.         self.view.showBoardNumbering(config.get('show_numbering') is True)
  92.         self.view.showSmooth(config.get('show_3d_smooth') is True)
  93.         self._ChessGame__movePlayer = player.MovePlayer(self)
  94.         self.addSpectator(self._ChessGame__movePlayer)
  95.         self.date = time.strftime('%Y.%m.%d')
  96.  
  97.     
  98.     def addAIPlayer(self, name, profile, level):
  99.         """Create an AI player.
  100.         
  101.         'name' is the name of the player to create (string).
  102.         'profile' is the the AI profile to use (ai.Profile).
  103.         'level' is the difficulty level to use (string).
  104.         
  105.         Returns an AI player to use (game.ChessPlayer).
  106.         """
  107.         description = _("'%(name)s' in '%(game)s'") % {
  108.             'name': name,
  109.             'game': self.name }
  110.         p = player.AIPlayer(self.application, name, profile, level, description)
  111.         self._ChessGame__aiPlayers.append(p)
  112.         self.application.watchAIPlayer(p)
  113.         return p
  114.  
  115.     
  116.     def addHumanPlayer(self, name):
  117.         """Create a human player.
  118.         
  119.         'name' is the name of the player to create.
  120.         
  121.         Returns a human player to use (game.ChessPlayer).
  122.         """
  123.         return player.HumanPlayer(self, name)
  124.  
  125.     
  126.     def setTimer(self, duration, whiteTime, blackTime):
  127.         self.duration = duration
  128.         if duration <= 0:
  129.             return None
  130.         self.view.controller.setWhiteTime(duration, whiteTime)
  131.         self.view.controller.setBlackTime(duration, blackTime)
  132.         self.wT = PlayerTimer(self, chess.board.WHITE, whiteTime)
  133.         self.bT = PlayerTimer(self, chess.board.BLACK, blackTime)
  134.         self.wT.controller.run()
  135.         self.setTimers(self.wT, self.bT)
  136.  
  137.     
  138.     def getHumanPlayer(self):
  139.         '''Get the human player.
  140.         
  141.         Returns the human player (HumanPlayer) or None if no human players.
  142.         If both players are human the current player is returned.
  143.         '''
  144.         c = self.getCurrentPlayer()
  145.         if isinstance(c, player.HumanPlayer):
  146.             return c
  147.         white = self.getWhite()
  148.         black = self.getWhite()
  149.         if c is white:
  150.             opponent = black
  151.         else:
  152.             opponent = white
  153.         if isinstance(opponent, player.HumanPlayer):
  154.             return opponent
  155.  
  156.     
  157.     def currentPlayerIsHuman(self):
  158.         '''Test if the player to move is human.
  159.  
  160.         Returns True if the current player is human and able to move.
  161.         '''
  162.         p = self.getCurrentPlayer()
  163.         if isinstance(p, player.HumanPlayer):
  164.             pass
  165.         return p.isReadyToMove()
  166.  
  167.     
  168.     def squareIsFriendly(self, coord):
  169.         '''
  170.         '''
  171.         owner = self.getSquareOwner(coord)
  172.         if owner is None:
  173.             return False
  174.         return owner is self.getCurrentPlayer()
  175.  
  176.     
  177.     def moveHuman(self, start, end):
  178.         '''
  179.         '''
  180.         if not self.currentPlayerIsHuman():
  181.             raise AssertionError
  182.         p = self.getCurrentPlayer()
  183.         if p is self.getWhite():
  184.             colour = chess.board.WHITE
  185.         else:
  186.             colour = chess.board.BLACK
  187.         
  188.         try:
  189.             promotionType = self._ChessGame__promotionMapping[config.get('promotion_type')]
  190.         except KeyError:
  191.             promotionType = chess.board.QUEEN
  192.  
  193.         move = chess.lan.encode(colour, start, end, promotionType = promotionType)
  194.         p.move(move)
  195.         self.view.controller.setAttention(False)
  196.  
  197.     
  198.     def toPGN(self, pgnGame):
  199.         """Write the properties of this game into a PGN game.
  200.         
  201.         'pgnGame' is the game to write into (pgn.PGNGame). All the tags should be unset.
  202.         """
  203.         white = self.getWhite()
  204.         black = self.getBlack()
  205.         pgnGame.setTag(chess.pgn.TAG_EVENT, self.name)
  206.         pgnGame.setTag(chess.pgn.TAG_WHITE, white.getName())
  207.         pgnGame.setTag(chess.pgn.TAG_BLACK, black.getName())
  208.         pgnGame.setTag(chess.pgn.TAG_DATE, self.date)
  209.         results = {
  210.             game.RESULT_WHITE_WINS: chess.pgn.RESULT_WHITE_WIN,
  211.             game.RESULT_BLACK_WINS: chess.pgn.RESULT_BLACK_WIN,
  212.             game.RESULT_DRAW: chess.pgn.RESULT_DRAW }
  213.         
  214.         try:
  215.             value = results[self.result]
  216.         except KeyError:
  217.             pass
  218.  
  219.         pgnGame.setTag(chess.pgn.TAG_RESULT, value)
  220.         rules = {
  221.             game.RULE_ABANDONMENT: chess.pgn.TERMINATE_ABANDONED,
  222.             game.RULE_TIMEOUT: chess.pgn.TERMINATE_TIME_FORFEIT,
  223.             game.RULE_DEATH: chess.pgn.TERMINATE_DEATH }
  224.         
  225.         try:
  226.             value = rules[self.rule]
  227.         except KeyError:
  228.             pass
  229.  
  230.         pgnGame.setTag(chess.pgn.TAG_TERMINATION, value)
  231.         if self.duration > 0:
  232.             pgnGame.setTag(chess.pgn.TAG_TIME_CONTROL, str(self.duration))
  233.         
  234.         if self.wT is not None:
  235.             pgnGame.setTag('WhiteTime', str(self.wT.controller.getRemaining()))
  236.         
  237.         if self.bT is not None:
  238.             pgnGame.setTag('BlackTime', str(self.bT.controller.getRemaining()))
  239.         
  240.         if isinstance(white, ai.Player):
  241.             (profile, level) = white.getProfile()
  242.             pgnGame.setTag('WhiteAI', profile)
  243.             pgnGame.setTag('WhiteLevel', level)
  244.         
  245.         if isinstance(black, ai.Player):
  246.             (profile, level) = black.getProfile()
  247.             pgnGame.setTag('BlackAI', profile)
  248.             pgnGame.setTag('BlackLevel', level)
  249.         
  250.         moves = self.getMoves()
  251.         for m in moves:
  252.             pgnMove = chess.pgn.PGNMove()
  253.             pgnMove.move = m.sanMove
  254.             pgnMove.nag = m.nag
  255.             pgnMove.comment = m.comment
  256.             pgnGame.addMove(pgnMove)
  257.         
  258.  
  259.     
  260.     def animate(self, timeStep):
  261.         '''
  262.         '''
  263.         return self.view.scene.controller.animate(timeStep)
  264.  
  265.     
  266.     def endMove(self, p):
  267.         game.ChessGame.endMove(self, p)
  268.         self.view.updateRotation()
  269.  
  270.     
  271.     def remove(self):
  272.         '''Remove this game'''
  273.         for p in self._ChessGame__aiPlayers:
  274.             p.window.close()
  275.             self.application.unwatchAIPlayer(p)
  276.         
  277.         self.abort()
  278.         self.application._removeGame(self)
  279.         self.view.controller.close()
  280.  
  281.     
  282.     def setNeedsSaving(self, needsSaving):
  283.         '''
  284.         '''
  285.         if self.inHistory:
  286.             needsSaving = False
  287.         
  288.         if self.needsSaving == needsSaving:
  289.             return None
  290.         self.needsSaving = needsSaving
  291.         self.view.controller.setNeedsSaving(needsSaving)
  292.  
  293.     
  294.     def save(self):
  295.         '''Save this game'''
  296.         pgnGame = chess.pgn.PGNGame()
  297.         self.toPGN(pgnGame)
  298.         if self.inHistory:
  299.             if len(self.getMoves()) < 2:
  300.                 return None
  301.             self.application.history.save(pgnGame, self.fileName)
  302.         else:
  303.             
  304.             try:
  305.                 f = file(self.fileName, 'w')
  306.                 lines = pgnGame.getLines()
  307.                 for line in lines:
  308.                     f.write(line + '\n')
  309.                 
  310.                 f.write('\n')
  311.                 f.close()
  312.             except IOError:
  313.                 e = None
  314.                 return e.strerror
  315.  
  316.         self.setNeedsSaving(False)
  317.         self.application.logger.addLine('Saved game %s to %s' % (repr(self.name), self.fileName))
  318.  
  319.  
  320.  
  321. class UI(ui.UIFeedback):
  322.     '''
  323.     '''
  324.     application = None
  325.     splashscreen = None
  326.     controller = None
  327.     
  328.     def __init__(self, application):
  329.         '''
  330.         '''
  331.         self.controller = gtkui.GtkUI(self)
  332.         self.application = application
  333.         self.splashscreen = display.Splashscreen(self)
  334.         self.splashscreen.controller = self.controller.setView('', self.splashscreen, isPlayable = False)
  335.         self.ggzConfig = network.GGZConfig()
  336.         dialog = network.GGZNetworkDialog(self)
  337.         self.networkDialog = dialog.controller = self.controller.addNetworkDialog(dialog)
  338.         for server in self.ggzConfig.getServers():
  339.             dialog.controller.addProfile(server, server.name)
  340.         
  341.  
  342.     
  343.     def onAnimate(self, timeStep):
  344.         '''Called by ui.UIFeedback'''
  345.         return self.application.animate(timeStep)
  346.  
  347.     
  348.     def onReadFileDescriptor(self, fd):
  349.         '''Called by ui.UIFeedback'''
  350.         
  351.         try:
  352.             handler = self.application.ioHandlers[fd]
  353.         except KeyError:
  354.             return False
  355.  
  356.         result = handler.read()
  357.         if result is False:
  358.             self.application.ioHandlers.pop(fd)
  359.         
  360.         return result
  361.  
  362.     
  363.     def onWriteFileDescriptor(self, fd):
  364.         '''Called by ui.UIFeedback'''
  365.         
  366.         try:
  367.             handler = self.application.ioHandlers[fd]
  368.         except KeyError:
  369.             return False
  370.  
  371.         result = handler.write()
  372.         if result is False:
  373.             self.application.ioHandlers.pop(fd)
  374.         
  375.         return result
  376.  
  377.     
  378.     def onGameStart(self, game):
  379.         '''Called by ui.UIFeedback'''
  380.         if game.white.type == '':
  381.             w = None
  382.         else:
  383.             w = (game.white.type, game.white.level)
  384.         if game.black.type == '':
  385.             b = None
  386.         else:
  387.             b = (game.black.type, game.black.level)
  388.         g = self.application.addLocalGame(game.name, game.white.name, w, game.black.name, b)
  389.         if g is None:
  390.             return None
  391.         g.inHistory = True
  392.         self.application.logger.addLine('Starting game %s between %s (%s) and %s (%s). (%i moves)' % (game.name, game.white.name, str(game.white.type), game.black.name, str(game.black.type), len(game.moves)))
  393.         g.setTimer(game.duration, game.duration, game.duration)
  394.         g.start(game.moves)
  395.  
  396.     
  397.     def loadGame(self, path, configure):
  398.         '''Called by ui.UI'''
  399.         
  400.         try:
  401.             p = chess.pgn.PGN(path, 1)
  402.         except chess.pgn.Error:
  403.             e = None
  404.             return e.message
  405.             except IOError:
  406.                 e = None
  407.                 return e.strerror
  408.             else:
  409.                 self.application.addPGNGame(p[0], path, configure)
  410.                 return None
  411.  
  412.  
  413.     
  414.     def onNewNetworkGame(self):
  415.         '''Called by ui.UIFeedback'''
  416.         self.networkDialog.setVisible(True)
  417.  
  418.     
  419.     def onQuit(self):
  420.         '''Called by ui.UIFeedback'''
  421.         self.application.quit()
  422.  
  423.  
  424.  
  425. class Application:
  426.     '''
  427.     '''
  428.     ui = None
  429.     __aiProfiles = None
  430.     ioHandlers = None
  431.     networkConnections = None
  432.     __detector = None
  433.     __game = None
  434.     
  435.     def __init__(self):
  436.         '''Constructor for glChess application'''
  437.         self._Application__aiProfiles = { }
  438.         self.ioHandlers = { }
  439.         self.networkConnections = { }
  440.         self._Application__detector = None
  441.         self.ui = UI(self)
  442.         self.history = history.GameHistory()
  443.         title = _('Application Log')
  444.         self.logger = self.ui.controller.addLogWindow(title, '', '')
  445.  
  446.     
  447.     def addAIProfile(self, profile):
  448.         """Add a new AI profile into glChess.
  449.         
  450.         'profile' is the profile to add (ai.Profile).
  451.         """
  452.         name = profile.name
  453.         if not self._Application__aiProfiles.has_key(name) is False:
  454.             raise AssertionError
  455.         self._Application__aiProfiles[name] = profile
  456.         self.ui.controller.addAIEngine(name)
  457.  
  458.     
  459.     def getAIProfile(self, name):
  460.         """Get an installed AI profile.
  461.         
  462.         'name' is the name of the profile to get (string).
  463.         
  464.         Return the profile (ai.Profile) or None if it does not exist.
  465.         """
  466.         
  467.         try:
  468.             return self._Application__aiProfiles[name]
  469.         except KeyError:
  470.             return None
  471.  
  472.  
  473.     
  474.     def watchAIPlayer(self, p):
  475.         '''
  476.         '''
  477.         fd = p.fileno()
  478.         if fd is not None:
  479.             self.ioHandlers[fd] = p
  480.             self.ui.controller.watchFileDescriptor(fd)
  481.         
  482.  
  483.     
  484.     def unwatchAIPlayer(self, p):
  485.         '''
  486.         '''
  487.         fd = p.fileno()
  488.         if fd is not None:
  489.             self.ioHandlers.pop(fd)
  490.         
  491.  
  492.     
  493.     def addGame(self, name):
  494.         if self._Application__game is not None:
  495.             if self._Application__game.inHistory:
  496.                 response = ui.SAVE_YES
  497.             elif self._Application__game.needsSaving:
  498.                 response = self.ui.controller.requestSave('Save current game?')
  499.             else:
  500.                 response = ui.SAVE_NO
  501.             if response is ui.SAVE_YES:
  502.                 self._Application__game.save()
  503.             elif response is ui.SAVE_ABORT:
  504.                 return None
  505.         
  506.         self._Application__game = ChessGame(self, name)
  507.         return self._Application__game
  508.  
  509.     
  510.     def addLocalGame(self, name, whiteName, whiteType, blackName, blackType):
  511.         """Add a chess game into glChess.
  512.         
  513.         'name' is the name of the game (string).
  514.         'whiteName' is the name of the white player (string).
  515.         'whiteType' is a 2-tuple containing the AI profile name and difficulty level (str, str) or None for human players.
  516.         'blackName' is the name of the black player (string).
  517.         'blackType' is a 2-tuple containing the AI profile name and difficulty level (str, str) or None for human players.
  518.         
  519.         Returns the game object. Use game.start() to start the game.
  520.         """
  521.         g = self.addGame(name)
  522.         if g is None:
  523.             return None
  524.         msg = ''
  525.         if whiteType is None:
  526.             p = g.addHumanPlayer(whiteName)
  527.         else:
  528.             (profile, level) = whiteType
  529.             p = g.addAIPlayer(whiteName, self._Application__aiProfiles[profile], level)
  530.         g.setWhite(p)
  531.         if blackType is None:
  532.             p = g.addHumanPlayer(blackName)
  533.         else:
  534.             (profile, level) = blackType
  535.             p = g.addAIPlayer(blackName, self._Application__aiProfiles[profile], level)
  536.         g.setBlack(p)
  537.         return g
  538.  
  539.     
  540.     def addPGNGame(self, pgnGame, path, configure = False):
  541.         """Add a PGN game.
  542.         
  543.         'pgnGame' is the game to add (chess.pgn.PGNGame).
  544.         'path' is the path this game was loaded from (string or None).
  545.         
  546.         Returns the game object. Use game.start() to start the game.
  547.         """
  548.         gameProperties = ui.Game()
  549.         gameProperties.path = path
  550.         gameProperties.name = pgnGame.getTag(chess.pgn.TAG_EVENT)
  551.         gameProperties.white.name = pgnGame.getTag(chess.pgn.TAG_WHITE)
  552.         gameProperties.black.name = pgnGame.getTag(chess.pgn.TAG_BLACK)
  553.         moves = []
  554.         for pgnMove in pgnGame.getMoves():
  555.             moves.append(pgnMove.move)
  556.         
  557.         gameProperties.moves = moves
  558.         missingEngines = False
  559.         gameProperties.white.type = pgnGame.getTag('WhiteAI', '')
  560.         if gameProperties.white.type == '':
  561.             w = None
  562.         elif not self._Application__aiProfiles.has_key(gameProperties.white.type):
  563.             missingEngines = True
  564.         
  565.         gameProperties.white.level = pgnGame.getTag('WhiteLevel')
  566.         if gameProperties.white.level is None:
  567.             gameProperties.white.level = 'normal'
  568.         
  569.         w = (gameProperties.white.type, gameProperties.white.level)
  570.         gameProperties.black.type = pgnGame.getTag('BlackAI', '')
  571.         if gameProperties.black.type == '':
  572.             b = None
  573.         elif not self._Application__aiProfiles.has_key(gameProperties.black.type):
  574.             missingEngines = True
  575.         
  576.         gameProperties.black.level = pgnGame.getTag('BlackLevel')
  577.         if gameProperties.black.level is None:
  578.             gameProperties.black.level = 'normal'
  579.         
  580.         b = (gameProperties.black.type, gameProperties.black.level)
  581.         if missingEngines or configure:
  582.             self.ui.controller.reportGameLoaded(gameProperties)
  583.             return None
  584.         newGame = self.addLocalGame(gameProperties.name, gameProperties.white.name, w, gameProperties.black.name, b)
  585.         if newGame is None:
  586.             return None
  587.         newGame.date = pgnGame.getTag(chess.pgn.TAG_DATE)
  588.         newGame.fileName = path
  589.         moves = newGame.getMoves()
  590.         pgnMoves = pgnGame.getMoves()
  591.         for i in xrange(len(moves)):
  592.             moves[i].comment = pgnMoves[i].comment
  593.             moves[i].nag = pgnMoves[i].nag
  594.         
  595.         result = pgnGame.getTag(chess.pgn.TAG_RESULT, None)
  596.         loser = None
  597.         if result == chess.pgn.RESULT_DRAW:
  598.             newGame.claimDraw()
  599.             if newGame.result != game.RESULT_DRAW:
  600.                 newGame.endGame(game.RESULT_DRAW, game.RULE_AGREEMENT)
  601.             
  602.         elif result == chess.pgn.RESULT_INCOMPLETE:
  603.             if newGame.result != game.RESULT_IN_PROGRESS:
  604.                 print "WARNING: PGN file specifies game in progress, glChess does't..."
  605.             
  606.         elif result == chess.pgn.RESULT_WHITE_WIN:
  607.             loser = newGame.getBlack()
  608.         elif result == chess.pgn.RESULT_BLACK_WIN:
  609.             loser = newGame.getWhite()
  610.         
  611.         if newGame.result == game.RESULT_IN_PROGRESS and loser is not None:
  612.             loser.resign()
  613.         
  614.         duration = 0
  615.         value = pgnGame.getTag(chess.pgn.TAG_TIME_CONTROL)
  616.         if value is not None:
  617.             timers = value.split(':')
  618.             
  619.             try:
  620.                 duration = int(timers[0])
  621.             except ValueError:
  622.                 print 'Unknown time control: ' + value
  623.             except:
  624.                 None<EXCEPTION MATCH>ValueError
  625.             
  626.  
  627.         None<EXCEPTION MATCH>ValueError
  628.         value = pgnGame.getTag('WhiteTime', duration * 1000)
  629.         
  630.         try:
  631.             whiteTime = int(value)
  632.         except ValueError:
  633.             whiteTime = duration
  634.  
  635.         value = pgnGame.getTag('BlackTime', duration * 1000)
  636.         
  637.         try:
  638.             blackTime = int(value)
  639.         except ValuError:
  640.             blackTime = duration
  641.  
  642.         newGame.setTimer(duration, whiteTime / 1000, blackTime / 1000)
  643.         newGame.setNeedsSaving(False)
  644.         return newGame
  645.  
  646.     
  647.     def start(self):
  648.         '''Run glChess.
  649.         
  650.         This method does not return.
  651.         '''
  652.         self.logger.addLine('This is glChess %s' % VERSION)
  653.         profiles = ai.loadProfiles()
  654.         for p in profiles:
  655.             p.detect()
  656.             if p.path is not None:
  657.                 self.logger.addLine('Detected AI: %s at %s' % (p.name, p.path))
  658.                 self.addAIProfile(p)
  659.                 continue
  660.         
  661.         nArgs = len(sys.argv)
  662.         if nArgs == 1:
  663.             self._Application__autoload()
  664.         elif nArgs == 2:
  665.             path = sys.argv[1]
  666.             import time
  667.             self.logger.addLine('loading...')
  668.             s = time.time()
  669.             
  670.             try:
  671.                 p = chess.pgn.PGN(path, 1)
  672.             except chess.pgn.Error:
  673.                 e = None
  674.                 self.logger.addLine('Unable to open PGN file %s: %s' % (path, str(e)))
  675.             except IOError:
  676.                 e = None
  677.                 self.logger.addLine('Unable to open PGN file %s: %s' % (path, str(e)))
  678.  
  679.             if len(p) > 0:
  680.                 g = self.addPGNGame(p[0], path)
  681.             
  682.             self.logger.addLine('loaded in %f seconds' % (time.time() - s))
  683.         else:
  684.             print _('Usage: %s [game]') % sys.argv[0]
  685.             sys.exit(0)
  686.         if self._Application__game is None and len(self._Application__aiProfiles) > 0:
  687.             for p in profiles:
  688.                 if self._Application__aiProfiles.has_key(p.name):
  689.                     aiName = p.name
  690.                     break
  691.                     continue
  692.             
  693.             black = (aiName, 'easy')
  694.             gameName = _('Human versus %s') % aiName
  695.             whiteName = _('White')
  696.             blackName = _('Black')
  697.             g = self.addLocalGame(gameName, whiteName, None, blackName, black)
  698.             g.inHistory = True
  699.             g.start()
  700.         
  701.         
  702.         try:
  703.             self.ui.controller.run()
  704.         except:
  705.             print _('glChess has crashed. Please report this bug to http://bugzilla.gnome.org\nDebug output:')
  706.             print traceback.format_exc()
  707.             self.quit()
  708.             sys.exit(1)
  709.  
  710.  
  711.     
  712.     def animate(self, timeStep):
  713.         '''
  714.         '''
  715.         return self._Application__game.animate(timeStep)
  716.  
  717.     
  718.     def quit(self):
  719.         '''Quit glChess'''
  720.         if self._Application__game is not None:
  721.             if self._Application__game.inHistory:
  722.                 response = ui.SAVE_YES
  723.             elif self._Application__game.needsSaving:
  724.                 response = self.ui.controller.requestSave(_('Save game before closing?'))
  725.             else:
  726.                 response = ui.SAVE_NO
  727.             if response == ui.SAVE_YES:
  728.                 self._Application__game.save()
  729.             elif response == ui.SAVE_ABORT:
  730.                 return None
  731.             self._Application__game.abort()
  732.         
  733.         self.ui.controller.close()
  734.         sys.exit()
  735.  
  736.     
  737.     def __autoload(self):
  738.         '''Restore games from the autosave file'''
  739.         (pgnGame, fileName, inHistory) = self.history.getUnfinishedGame()
  740.         if pgnGame is not None:
  741.             g = self.addPGNGame(pgnGame, fileName)
  742.             if g is not None:
  743.                 g.inHistory = inHistory
  744.             
  745.         
  746.  
  747.  
  748. if __name__ == '__main__':
  749.     app = Application()
  750.     app.start()
  751.  
  752.